<template>
<div style="box-shadow: 5px 5px 10px 0px  rgba(0, 0, 0, 0.2);">
    <br>
    <el-form :inline="true" :model="businessSearch" class="demo-form-inline">
    <el-form-item label="商家账号">
      <el-input v-model="businessSearch.account" placeholder="员工账号" clearable />
    </el-form-item>
    <el-form-item label="商家电话">
      <el-input v-model="businessSearch.telephone" placeholder="手机号" clearable />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="getPage">搜索</el-button>
      
    </el-form-item>
  </el-form>
  <el-divider />

  <el-table :data="businesses" stripe style="width: 100%">
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column prop="account" label="账号"  />
      <el-table-column prop="concat" label="联系人姓名"  />
      <el-table-column prop="telephone" label="手机号" />
      <el-table-column prop="reg_time" label="商家注册时间" />
      <el-table-column  label="操作" >
        <template #default="scope">
          <el-button size="small"  @click="edit(scope.row)">
            修改
          </el-button>
          <el-button size="small" type="danger"  @click="deleteB(scope.row)">
            删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>
  <el-divider />


    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[2, 10, 15, 20]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />


  <el-dialog v-model="dialogFormVisible" :title="dTitle" width="500">
    <el-form :model="business">
      <el-form-item label="商家账号" >
        <el-input v-model="business.account" autocomplete="off" />
      </el-form-item>
      <el-form-item label="联系人" >
        <el-input v-model="business.contact" autocomplete="off" />
      </el-form-item>
      <el-form-item label="电话" >
        <el-input v-model="business.telephone" autocomplete="off" />
      </el-form-item>   
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="onSubmit">
          Confirm
        </el-button>
      </div>
    </template>
  </el-dialog>
  <br>
  </div>

</template>

<script setup>
import axios from 'axios';
import { ref, reactive, onBeforeMount } from 'vue'
import qs from 'qs'
import { ElMessage,ElMessageBox } from 'element-plus'
const businessSearch=reactive({
  account:'',
  telephone:''
})
const businesses=ref([])
const total=ref(0)
const currentPage=ref(1)
const pageSize=ref(2)
const dialogFormVisible=ref(false)
const business=reactive({
    id:'',
  account:'',
  contact:'',
  telephone:''
})
const dTitle=ref('')
const edit=(row)=>{
    business.account=row.account;
    business.contact=row.contact;
    business.telephone=row.telephone;
    business.id=row.id;
    dTitle.value='修改商家信息';
    dialogFormVisible.value=true;
}
const onSubmit=()=>{
    console.log(business);
    axios.post('http://localhost:7444/business/updateBusiness',business).then((response)=>{
      if(response.data.code==200){
        ElMessage({
          message: '修改成功',
          type: 'success',
        });
        dialogFormVisible.value=false;
        getPage();
      }
    })
}
const deleteB=(row)=>{
  ElMessageBox.confirm(
    '确定要删除该商家吗',
    'Warning',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',     
      type: 'warning',
    }
  ).then(() => {
    axios.put('http://localhost:7444/business/deleteBusiness/'+row.id).then(response => {
      if (response.data.code === 200) {
        ElMessage({
          message: '删除成功.',
           type: 'success',
          plain: true,
          })
          getPage();
      }else{
        ElMessage({
          message: '删除出错',
        })
      }
   })
  }).catch(() => {
    ElMessage({
      message: '取消修改',
      type: 'info',
      plain: true,
    })
  });
}
const getPage=()=>{
    axios.get('http://localhost:7444/business/pageSelectBusiness?'+qs.stringify({
      pageNum:currentPage.value,
      pageSize:pageSize.value,
      account:businessSearch.account,
      telephone:businessSearch.telephone
    })).then((response)=>{
      if(response.data.code==200){
        businesses.value=response.data.data.list;
        total.value = response.data.data.totalCount;
      }
    })
}
const handleSizeChange = (val) => {
  console.log(val);
  pageSize.value = val; 
  getPage(); 
};
const handleCurrentChange = (val) => {
  console.log(val);
  currentPage.value = val;
  getPage(); 
};
onBeforeMount(()=>{
    getPage();
})
</script>

<style lang="scss" scoped>

</style>